// server.js
const express = require('express');
const app = express();
const port = 3000;

// 允许跨域请求
const cors = require('cors');
// app.use(cors());
app.use(cors({
    origin: 'http://localhost:5173',  // 允许的前端源
    credentials: true,  // 允许携带凭证（如 cookies）
}));

// 解析 JSON 请求体
app.use(express.json());

// 示例接口：返回请求的参数和当前时间
app.get('/api/hello', (req, res) => {
    // const response = {
    //     message: "Hello, world!",
    //     timestamp: new Date().toISOString(),
    //     queryParams: req.query
    // };
    const response ={
        "status": 0,
        "msg": "请求成功",
        "data": {
          "name":"杨乐"
        }
      }
    res.json(response);
});

// 示例 POST 接口：接收数据并返回
app.post('/api/data', (req, res) => {
    const data = req.body; // 获取请求体中的数据
    res.json({
        receivedData: data,
        status: "success",
        message: "Data received successfully"
    });
});

// 启动服务
app.listen(port, () => {
    console.log(`Server running at http://localhost:${port}`);
});
